﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using AccessKeyHighlighterControl;

namespace UnitTests
{
    /// <summary>
    /// Contains tests for the AccessKeyHighlighter project
    /// </summary>
    [TestClass]
    public class AccessKeyHighlighterTests
    {
        public AccessKeyHighlighterTests()
        { }

        public TestContext TestContext { get; set; }

        [TestMethod]
        [Description("Tests that the InjectAccessKeyMarkup method correctly marks up the access key when it is declared on the form field and the first letter in the label")]
        public void InjectAccessKeyMarkupLabelForTextBoxWithAccessKeyAsFirstLetter()
        {
            AccessKeyHighlighter akh = new AccessKeyHighlighter();
            
            Page page = new Page();
            Label lblFirstName = new Label() { ID="lblFirstName", Text="First Name:", AssociatedControlID="txtFirstName" };
            page.Controls.Add(lblFirstName);
            page.Controls.Add(new TextBox() { ID = "txtFirstName", AccessKey = "F" });
            page.Controls.Add(akh);

            Helpers.InjectAccessKeyMarkup(akh);
            
            string expected = "<em>F</em>irst Name:";
            Assert.AreEqual<string>(expected, lblFirstName.Text, "Label text is not what was expected");
        }

        [TestMethod]
        [Description("Tests that the InjectAccessKeyMarkup method correctly marks up the access key when it is declared on the form field and in the middle of the label")]
        public void InjectAccessKeyMarkupLabelForTextBoxWithAccessKeyAsMiddleLetter()
        {
            AccessKeyHighlighter akh = new AccessKeyHighlighter();
            
            Page page = new Page();
            Label lblFirstName = new Label() { ID = "lblPostCode", Text = "Post Code:", AssociatedControlID = "txtPostCode" };
            page.Controls.Add(lblFirstName);
            page.Controls.Add(new TextBox() { ID = "txtPostCode", AccessKey = "C" });
            page.Controls.Add(akh);

            Helpers.InjectAccessKeyMarkup(akh);

            string expected = "Post <em>C</em>ode:";
            Assert.AreEqual<string>(expected, lblFirstName.Text, "Label text is not what was expected");
        }

        [TestMethod]
        [Description("Tests that the InjectAccessKeyMarkup method correctly marks up the access key when it is declared on the form field and the last letter of the label")]
        public void InjectAccessKeyMarkupLabelForTextBoxWithAccessKeyAsLastLetter()
        {
            AccessKeyHighlighter akh = new AccessKeyHighlighter();

            Page page = new Page();
            Label lblFirstName = new Label() { ID = "lblPhone", Text = "Phone:", AssociatedControlID = "txtPhone" };
            page.Controls.Add(lblFirstName);
            page.Controls.Add(new TextBox() { ID = "txtPhone", AccessKey = "e" });
            page.Controls.Add(akh);

            Helpers.InjectAccessKeyMarkup(akh);

            string expected = "Phon<em>e</em>:";
            Assert.AreEqual<string>(expected, lblFirstName.Text, "Label text is not what was expected");
        }

        [TestMethod]
        [Description("Tests that the InjectAccessKeyMarkup method correctly marks up the access key when it is declared on the label and is the first letter in the label")]
        public void InjectAccessKeyMarkupLabelWithAccessKeyForTextBoxAsFirstLetter()
        {
            AccessKeyHighlighter akh = new AccessKeyHighlighter();

            Page page = new Page();
            Label lblFirstName = new Label() { ID = "lblFirstName", Text = "First Name:", AssociatedControlID = "txtFirstName", AccessKey = "F" };
            page.Controls.Add(lblFirstName);
            page.Controls.Add(new TextBox() { ID = "txtFirstName" });
            page.Controls.Add(akh);

            Helpers.InjectAccessKeyMarkup(akh);

            string expected = "<em>F</em>irst Name:";
            Assert.AreEqual<string>(expected, lblFirstName.Text, "Label text is not what was expected");
        }

        [TestMethod]
        [Description("Tests that the InjectAccessKeyMarkup method correctly marks up the access key when it is declared on the label and is in the middle of the label")]
        public void InjectAccessKeyMarkupLabelWithAccessKeyForTextBoxMiddleLetter()
        {
            AccessKeyHighlighter akh = new AccessKeyHighlighter();

            Page page = new Page();
            Label lblFirstName = new Label() { ID = "lblPostCode", Text = "Post Code:", AssociatedControlID = "txtPostCode", AccessKey = "C" };
            page.Controls.Add(lblFirstName);
            page.Controls.Add(new TextBox() { ID = "txtPostCode" });
            page.Controls.Add(akh);

            Helpers.InjectAccessKeyMarkup(akh);

            string expected = "Post <em>C</em>ode:";
            Assert.AreEqual<string>(expected, lblFirstName.Text, "Label text is not what was expected");
        }

        [TestMethod]
        [Description("Tests that the InjectAccessKeyMarkup method correctly marks up the access key when it is declared on the label and is the last letter of the label")]
        public void InjectAccessKeyMarkupLabelWithAccessKeyAsForTextBoxLastLetter()
        {
            AccessKeyHighlighter akh = new AccessKeyHighlighter();

            Page page = new Page();
            Label lblFirstName = new Label() { ID = "lblPhone", Text = "Phone:", AssociatedControlID = "txtPhone", AccessKey = "e" };
            page.Controls.Add(lblFirstName);
            page.Controls.Add(new TextBox() { ID = "txtPhone" });
            page.Controls.Add(akh);

            Helpers.InjectAccessKeyMarkup(akh);

            string expected = "Phon<em>e</em>:";
            Assert.AreEqual<string>(expected, lblFirstName.Text, "Label text is not what was expected");
        }

        [TestMethod]
        [Description("Tests that the InjectAccessKeyMarkup method correctly marks up the access key when it is a numeric character")]
        public void InjectAccessKeyMarkupLabelForTextBoxWithAccessKeyAsNumeric()
        {
            AccessKeyHighlighter akh = new AccessKeyHighlighter();

            Page page = new Page();
            Label lblFirstName = new Label() { ID = "lblStreet1", Text = "Street 1:", AssociatedControlID = "txtStreet1" };
            page.Controls.Add(lblFirstName);
            page.Controls.Add(new TextBox() { ID = "txtStreet1", AccessKey = "1" });
            page.Controls.Add(akh);

            Helpers.InjectAccessKeyMarkup(akh);

            string expected = "Street <em>1</em>:";
            Assert.AreEqual<string>(expected, lblFirstName.Text, "Label text is not what was expected");
        }

        [TestMethod]
        [Description("Tests that the InjectAccessKeyMarkup method correctly marks up the access key when it is declared on both the label and the form field")]
        public void InjectAccessKeyMarkupFormFieldAccessKeyPrecedence()
        {
            AccessKeyHighlighter akh = new AccessKeyHighlighter();

            Page page = new Page();
            Label lblFirstName = new Label() { ID = "lblFirstName", Text = "First Name:", AssociatedControlID = "txtFirstName", AccessKey = "N" };
            page.Controls.Add(lblFirstName);
            page.Controls.Add(new TextBox() { ID = "txtFirstName", AccessKey = "F" });
            page.Controls.Add(akh);

            Helpers.InjectAccessKeyMarkup(akh);

            string expected = "<em>F</em>irst Name:";
            Assert.AreEqual<string>(expected, lblFirstName.Text, "Label text is not what was expected");
        }

        [TestMethod]
        [Description("Tests that the InjectAccessKeyMarkup method correctly marks up the access key when called multiple times, e.g. across postbacks")]
        public void InjectAccessKeyMarkupMultpleTimes()
        {
            AccessKeyHighlighter akh = new AccessKeyHighlighter();

            Page page = new Page();
            Label lblFirstName = new Label() { ID = "lblFirstName", Text = "First Name:", AssociatedControlID = "txtFirstName" };
            page.Controls.Add(lblFirstName);
            page.Controls.Add(new TextBox() { ID = "txtFirstName", AccessKey = "F" });
            page.Controls.Add(akh);

            Helpers.InjectAccessKeyMarkup(akh);
            Helpers.InjectAccessKeyMarkup(akh);
            Helpers.InjectAccessKeyMarkup(akh);

            string expected = "<em>F</em>irst Name:";
            Assert.AreEqual<string>(expected, lblFirstName.Text, "Label text is not what was expected");
        }
    }
}